package com.sisi.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtils {
	private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
	public static final  Pattern DATE_FORMAT_CHINA1 = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}");
	public static final  Pattern DATE_FORMAT_CHINA2 = Pattern.compile("^\\d{4}-\\d{1}-\\d{1}");
	public static final  Pattern DATE_FORMAT_CHINA3 = Pattern.compile("^\\d{4}-\\d{1}-\\d{2}");
	public static final  Pattern DATE_FORMAT_CHINA4 = Pattern.compile("^\\d{4}-\\d{2}-\\d{1}");
	
	public static final  Pattern DATE_FORMAT_FOREIGN1 = Pattern.compile("^\\d{4}/\\d{2}/\\d{2}");
	public static final  Pattern DATE_FORMAT_FOREIGN2 = Pattern.compile("^\\d{4}/\\d{1}/\\d{1}");
	public static final  Pattern DATE_FORMAT_FOREIGN3 = Pattern.compile("^\\d{4}/\\d{1}/\\d{2}");
	public static final  Pattern DATE_FORMAT_FOREIGN4 = Pattern.compile("^\\d{4}/\\d{2}/\\d{1}");
	
	private static Map<String,String> dateRegFormat = null;
	private static Map<String,String> timeRegFormat = null;
	
	static{
		timeRegFormat = new HashMap<String, String>();
		timeRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$",
                "yyyy-MM-dd-HH-mm-ss");   //yyyy-MM-dd HH:mm:ss , yyyy/MM/dd HH:mm:ss
		
		timeRegFormat.put("^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$",
				"yyyy-MM-dd-HH-mm"); //yyyy-MM-dd HH:mm , yyyy/MM/dd HH:mm
		
		timeRegFormat.put("^\\d{4}\\D+\\d{1,2}\\D+\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}$",
				"yyyy-MM-ddHH-mm-ss"); //yyyy-MM-ddHH:mm:ss , yyyy/MM/ddHH:mm:ss
		
		timeRegFormat.put("^\\d{4}\\D+\\d{2}\\D+\\d{4}\\D+\\d{2}$",
                "yyyy-MM-ddHH-mm");  //yyyy-MM-ddHH:mm , yyyy/MM/ddHH:mm
        
		timeRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{6}$",
                "yyyy-MM-dd-HHmmss");   //yyyy-MM-dd HHmmss , yyyy/MM/dd HHmmss
		timeRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{4}$",
                "yyyy-MM-dd-HHmm");   //yyyy-MM-dd HHmm , yyyy/MM/dd HHmm
		timeRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{8}$",
                "yyyy-MM-ddHHmmss");   //yyyy-MM-ddHHmmss , yyyy/MM/ddHHmmss
		timeRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{6}$",
                "yyyy-MM-ddHHmm");   //yyyy-MM-ddHHmm , yyyy/MM/ddHHmm
        
		timeRegFormat.put(
                "^\\d{8}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$",
                "yyyyMMdd-HH-mm-ss");   //yyyyMMdd HH:mm:ss , yyyyMMdd HH:mm:ss
		timeRegFormat.put(
                "^\\d{8}\\D+\\d{1,2}\\D+\\d{1,2}$",
                "yyyyMMdd-HH-mm");   //yyyyMMdd HH:mm , yyyyMMdd HH:mm
		timeRegFormat.put(
                "^\\d{10}\\D+\\d{1,2}\\D+\\d{1,2}$",
                "yyyyMMddHH-mm-ss");   //yyyyMMddHH:mm:ss , yyyyMMddHH:mm:ss
		timeRegFormat.put(
                "^\\d{10}\\D+\\d{1,2}$",
                "yyyyMMddHH-mm");   //yyyyMMddHH:mm , yyyyMMddHH:mm
		timeRegFormat.put(
                "^\\d{8}\\D+\\d{6}$",
                "yyyyMMdd-HHmmss");   //yyyyMMdd HHmmss , yyyyMMdd HHmmss
		timeRegFormat.put(
                "^\\d{8}\\D+\\d{4}$",
                "yyyyMMdd-HHmm");   //yyyyMMdd HHmm , yyyyMMdd HHmm
		timeRegFormat.put("^\\d{14}$", "yyyyMMddHHmmss");//yyyyMMddHHmmss
		timeRegFormat.put("^\\d{12}$", "yyyyMMddHHmm");//yyyyMMddHHmm
		timeRegFormat.put("^\\d{10}$", "yyyyMMddHH");//yyyyMMddHH
		
		timeRegFormat.put("^\\d{4}$", "HHmm");//HHmm
		timeRegFormat.put("^\\d{3}$", "Hmm"); //Hmm
		timeRegFormat.put("^\\d{6}$", "HHmmss");//HHmmss
		timeRegFormat.put("^\\d{1,2}\\D+\\d{1,2}$", "HH-mm");//HH:mm
		timeRegFormat.put("^\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$", "HH-mm-ss");//HH:mm
		timeRegFormat.put("^\\d{6}\\D+\\d{2}\\D+\\d{2}$", "yyMMdd-HH-mm"); //yyMMdd HH:mm
	}
	
	static{
		dateRegFormat = new HashMap<String, String>();

	   /* dateRegFormat.put("^\\d{4}$", "yyyy");//yyyy
*/	    dateRegFormat.put("^\\d{4}\\D+\\d{2}$", "yyyy-MM");//yyyy-MM , yyyy/MM
	    dateRegFormat.put("^\\d{4}\\D+\\d{1}$", "yyyy-M");//yyyy-M , yyyy/M
        dateRegFormat.put("^\\d{6}$", "yyyyMM");//201403
		dateRegFormat.put("^\\d{4}\\D+\\d{2}\\D+\\d{2}$", "yyyy-MM-dd");//yyyy-MM-dd , yyyy/MM/dd
		dateRegFormat.put("^\\d{4}\\D+\\d{1}\\D+\\d{2}$", "yyyy-M-dd");//yyyy-M-dd , yyyy/M/dd
		dateRegFormat.put("^\\d{4}\\D+\\d{2}\\D+\\d{1}$", "yyyy-MM-d");//yyyy-MM-d , yyyy/MM/d
		dateRegFormat.put("^\\d{4}\\D+\\d{1}\\D+\\d{1}$", "yyyy-M-d");//yyyy-M-d , yyyy/M/d
		dateRegFormat.put("^\\d{8}$", "yyyyMMdd");//yyyyMMdd
		
		dateRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$",
                "yyyy-MM-dd-HH-mm-ss");   //yyyy-MM-dd HH:mm:ss , yyyy/MM/dd HH:mm:ss
		
		dateRegFormat.put("^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$",
				"yyyy-MM-dd-HH-mm"); //yyyy-MM-dd HH:mm , yyyy/MM/dd HH:mm
		
		dateRegFormat.put("^\\d{4}\\D+\\d{1,2}\\D+\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}$",
				"yyyy-MM-ddHH-mm-ss"); //yyyy-MM-ddHH:mm:ss , yyyy/MM/ddHH:mm:ss
		
        dateRegFormat.put("^\\d{4}\\D+\\d{2}\\D+\\d{4}\\D+\\d{2}$",
                "yyyy-MM-ddHH-mm");  //yyyy-MM-ddHH:mm , yyyy/MM/ddHH:mm
        
        dateRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{6}$",
                "yyyy-MM-dd-HHmmss");   //yyyy-MM-dd HHmmss , yyyy/MM/dd HHmmss
        dateRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{4}$",
                "yyyy-MM-dd-HHmm");   //yyyy-MM-dd HHmm , yyyy/MM/dd HHmm
        dateRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{8}$",
                "yyyy-MM-ddHHmmss");   //yyyy-MM-ddHHmmss , yyyy/MM/ddHHmmss
        dateRegFormat.put(
                "^\\d{4}\\D+\\d{1,2}\\D+\\d{6}$",
                "yyyy-MM-ddHHmm");   //yyyy-MM-ddHHmm , yyyy/MM/ddHHmm
        
        dateRegFormat.put(
                "^\\d{8}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}$",
                "yyyyMMdd-HH-mm-ss");   //yyyyMMdd HH:mm:ss , yyyyMMdd HH:mm:ss
        dateRegFormat.put(
                "^\\d{8}\\D+\\d{1,2}\\D+\\d{1,2}$",
                "yyyyMMdd-HH-mm");   //yyyyMMdd HH:mm , yyyyMMdd HH:mm
        dateRegFormat.put(
                "^\\d{10}\\D+\\d{1,2}\\D+\\d{1,2}$",
                "yyyyMMddHH-mm-ss");   //yyyyMMddHH:mm:ss , yyyyMMddHH:mm:ss
        dateRegFormat.put(
                "^\\d{10}\\D+\\d{1,2}$",
                "yyyyMMddHH-mm");   //yyyyMMddHH:mm , yyyyMMddHH:mm
        dateRegFormat.put(
                "^\\d{8}\\D+\\d{6}$",
                "yyyyMMdd-HHmmss");   //yyyyMMdd HHmmss , yyyyMMdd HHmmss
        dateRegFormat.put(
                "^\\d{8}\\D+\\d{4}$",
                "yyyyMMdd-HHmm");   //yyyyMMdd HHmm , yyyyMMdd HHmm
        dateRegFormat.put("^\\d{14}$", "yyyyMMddHHmmss");//yyyyMMddHHmmss
        dateRegFormat.put("^\\d{12}$", "yyyyMMddHHmm");//yyyyMMddHHmm
        dateRegFormat.put("^\\d{10}$", "yyyyMMddHH");//yyyyMMddHH
        dateRegFormat.put("^\\d{6}\\D+\\d{2}\\D+\\d{2}$", "yyMMdd-HH-mm"); //yyMMdd HH:mm
	}
	
	/**
	 * 格式化日期为字符串
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String dateFomat(Date date,String formatStr){
		ObjectUtils.requireNotNull(date);
		ObjectUtils.requireNotEmpty(formatStr);
		DateFormat df = new SimpleDateFormat(formatStr);
	    return df.format(date);

	}
	
	/**
	 * 将字符串格式化成Date，若没有匹配则返回null
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date formatDate(String dateStr) throws ParseException {
		 DateFormat formatter2;
		 String dateReplace;
		 for (String key : dateRegFormat.keySet()) {
             if (Pattern.compile(key).matcher(dateStr).matches()) {
            	 if(dateRegFormat.get(key).equals("yyyyMMdd")){
            		 String dt = dateStr.substring(4, 6);
            		 if(Integer.valueOf(dt) > 12){
            			 dateStr = dateStr.substring(4) + dateStr.substring(0,4);
            		 }
            	 }
            	 
                 formatter2 = new SimpleDateFormat(dateRegFormat.get(key));
                 dateReplace = dateStr.replaceAll("\\D+", "-");
                
                return formatter2.parse(dateReplace);
             }
         }
		
		return null;
	}
	
	public static Date formatTime(String timeStr) throws ParseException{
		DateFormat formatter2;
		 String dateReplace;
		 for (String key : timeRegFormat.keySet()) {
            if (Pattern.compile(key).matcher(timeStr).matches()) {
                formatter2 = new SimpleDateFormat(timeRegFormat.get(key));
                dateReplace = timeStr.replaceAll("\\D+", "-");
               return formatter2.parse(dateReplace);
            }
        }
		 
		return null;
	}
	@SuppressWarnings("finally")
    public static String formatDateStr(String dateStr){
		
        DateFormat formatter1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat formatter2;
        String dateReplace;
        String strSuccess="";
        try {
            for (String key : dateRegFormat.keySet()) {
                if (Pattern.compile(key).matcher(dateStr).matches()) {
                    formatter2 = new SimpleDateFormat(dateRegFormat.get(key));
                    dateReplace = dateStr.replaceAll("\\D+", "-");
                    strSuccess = formatter1.format(formatter2.parse(dateReplace));
                    break;
                }
            }
        } catch (Exception e) {
            System.err.println("-----------------日期格式无效:"+dateStr);
            throw e;
        } finally {
            return strSuccess;
        }
    }
    public static void main(String[] args) throws Exception{
    	
  /*  for(int i = 0 ;i<10000;i++){
    		new Thread(new Runnable() {
				
				@Override
				public void run() {
					 String[] dateStrArray=new String[]{
				               "2015",
				               "2015-06",
				               "2015-5",
				               "2015/5",
				               "2015/06",
				               "2015-06-12",
				               "2015/06/12",
				               "2015-6-12",
				               "2015/6/12",
				               "2015/06/2",
				               "2015-06-2",
				               "2015-6-2",
				               "2015/6/2",
				               "20150506",
				               "2015-06-12 12:05:13",
				               "2015/06/12 12:05:13",
				               "2015-06-12 12:05",
				               "2015/06/12 12:05",
				               "2015-06-1212:05:13",
				               "2015/06/1212:05:13",
				               "2015-06-1212:05",
				               "2015/06/1212:05",
				               "2015-06-12 120513",
				               "2015/06/12 120513",
				               "2015-06-12 1205",
				               "2015/06/12 1205",
				               "2015-06-12120513",
				               "2015/06/12120513",
				               "2015-06-121205",
				               "2015/06/121205",
				               "20150603 12:05:13",
				               "20150603 12:05",
				               "2015060312:05:13",
				               "2015060312:05",
				               "20150603 120513",
				               "20150603 1205",
				               "20150603120513",
				               "201506031205",
				               "2015060312",
				               "160512 13:00"
				               
				        };
				        for(int i=0;i<dateStrArray.length;i++){
				            try {
								System.out.println(dateStrArray[i] +"------------------------------".substring(1,30-dateStrArray[i].length())+ formatDate(dateStrArray[i]));
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
				        }
				        
				        String[] dateStrArray1=new String[]{
				               "23:12:12",
				               "23:12",
				               "152425",
				               "2123",
				               "160512 13:00"
				         };
				         for(int i=0;i<dateStrArray1.length;i++){
				             try {
								System.out.println(dateStrArray1[i] +"------------------------------".substring(1,30-dateStrArray1[i].length())+ formatTime(dateStrArray1[i]));
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
				         }
				         
				         try {
							
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}
			}).start();
    	
    	String[] dateStrArray=new String[]{
	               "2015",
	               "2015-06",
	               "2015-5",
	               "2015/5",
	               "2015/06",
	               "2015-06-12",
	               "2015/06/12",
	               "2015-6-12",
	               "2015/6/12",
	               "2015/06/2",
	               "2015-06-2",
	               "2015-6-2",
	               "2015/6/2",
	               "20150506",
	               "2015-06-12 12:05:13",
	               "2015/06/12 12:05:13",
	               "2015-06-12 12:05",
	               "2015/06/12 12:05",
	               "2015-06-1212:05:13",
	               "2015/06/1212:05:13",
	               "2015-06-1212:05",
	               "2015/06/1212:05",
	               "2015-06-12 120513",
	               "2015/06/12 120513",
	               "2015-06-12 1205",
	               "2015/06/12 1205",
	               "2015-06-12120513",
	               "2015/06/12120513",
	               "2015-06-121205",
	               "2015/06/121205",
	               "20150603 12:05:13",
	               "20150603 12:05",
	               "2015060312:05:13",
	               "2015060312:05",
	               "20150603 120513",
	               "20150603 1205",
	               "20150603120513",
	               "201506031205",
	               "2015060312",
	               "160512 13:00"
	               
	        };*/
    	System.out.println(formatTime("820"));
    	System.out.println(formatDate("13072016"));
	      /*  for(int i=0;i<dateStrArray.length;i++){
	            try {
					System.out.println(dateStrArray[i] +"------------------------------".substring(1,30-dateStrArray[i].length())+ formatDate(dateStrArray[i]));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	        
	        String[] dateStrArray1=new String[]{
	               "23:12:12",
	               "23:12",
	               "152425",
	               "2123",
	               "160512 13:00"
	         };
	         for(int i=0;i<dateStrArray1.length;i++){
	             try {
					System.out.println(dateStrArray1[i] +"------------------------------".substring(1,30-dateStrArray1[i].length())+ formatTime(dateStrArray1[i]));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	         }
	         
	         try {
				System.out.println(formatTime("12:33"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
         
    }
 
}
